package com.yihaodian.search.query.converter;

import java.util.List;

import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;

public class BooleanQueryConverter {
	public static BooleanQuery convert(
			com.yihaodian.api.search.query.BooleanQuery bq) {
		BooleanQuery ret = new BooleanQuery();
		
		List<com.yihaodian.api.search.query.BooleanQuery.BooleanClause> bcs = bq.getClauses();
		if(bcs != null) {
			for(com.yihaodian.api.search.query.BooleanQuery.BooleanClause bc : bcs) {
				Query q = QueryConverter.convert(bc.getQuery());
				Occur o = convert(bc.getOccur());
				ret.add(q, o);
			}
		}
		return ret;
	}
	
	private static Occur convert(com.yihaodian.api.search.query.BooleanQuery.BooleanClause.Occur o) {
		Occur ret = null;
		switch(o) {
		case MUST:
			ret = Occur.MUST;
			break;
		case MUST_NOT:
			ret = Occur.MUST_NOT;
			break;
		case SHOULD:
			ret = Occur.SHOULD;
			break;
		}
		
		return ret;
		
	}
}
